package controller.user;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import bean.User;
import service.UserService;
import service.impl.UserServiceImpl;
import utils.AJAXResult;
import utils.ResponseBodyUtil;


public class Insert extends HttpServlet {
	private static final long serialVersionUID = 1L;

	private UserService userService = new UserServiceImpl();
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		AJAXResult result = new AJAXResult();
		String loginacct = request.getParameter("loginacct");
		String username = request.getParameter("username");
		String email = request.getParameter("email");
		User user = new User();
		user.setLoginacct(loginacct);
		user.setUsername(username);
		user.setEmail(email);
		try {			
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			user.setCreatetime(sdf.format(new Date()));
			user.setUserpswd("123456");
			userService.insertUser(user);
			result.setSuccess(true);
		} catch ( Exception e ) {
			e.printStackTrace();
			result.setSuccess(false);
		}
		
		ResponseBodyUtil.sendJsonObject(response, result);
	}

}
